$(function(){
	$('#tabs').tabs();
});

var diapason = 0;
var longVib = 0;
var j = 0;
var nbreFrets = 0;
var t = 0;
	
	/*----------------------------Init purpose-------------------------*/
	if(!localStorage.decimalPrecision){
		localStorage.decimalPrecision = 1000;
	}
	if(!localStorage.scaleLength){
		localStorage.scaleLength = 635;
	}
	if(!localStorage.nbreFret){
		localStorage.nbreFret = 22;
	}
	if(!localStorage.cordier_max){
		localStorage.cordier_max = 17;
	}
	if(!localStorage.cordier_min){
		localStorage.cordier_min = 13;
	}
	if(!localStorage.fingerNA){
		localStorage.fingerNA = 5;
	}
	
	function init(){
		$("#diapas").val(localStorage.scaleLength);
		$("#diapasNA").val(localStorage.scaleLength);
		var select = document.getElementById("nbreFret");
		for (var i = 0; i < select.children.length; i++) {
			var child = select.children[i];
			if (child.value == localStorage.nbreFret) {
				child.selected = "true";
				break;
			}
		}
		var select = document.getElementById("nbreFretNA");
		for (var i = 0; i < select.children.length; i++) {
			var child = select.children[i];
			if (child.value == localStorage.nbreFret) {
				child.selected = "true";
				break;
			}
		}
		$("#cordier_max").val(localStorage.cordier_max);
		$("#cordier_min").val(localStorage.cordier_min);
		$("#fingerNA").val(localStorage.fingerNA);
	}

	/*---------------------------END------------------------------------*/

function foo(){
	$("#affichage").empty();
	diapason = $("#diapas").val();
	longVib = $("#diapas").val();
	nbreFrets = $("#nbreFret").val();
	j = 0;
	
	$("#affichage").append('<table><tr>');
	$("#affichage").append('<th>Fret</th>');
	$("#affichage").append('<th>form nut</th>');
	$("#affichage").append('<th>fret to fret</th></tr>');
	for(i=1;i<=nbreFrets;i++){
		longVib = calcul(longVib);
		$("#affichage").append('<tr>');
		$("#affichage").append('<td>' + i + '</td>');
		$("#affichage").append('<td>' + arrondir((diapason - longVib)) + '</td>');
		$("#affichage").append('<td>' + arrondir(((diapason - longVib) - j)) + '</td>');
		$("#affichage").append('</tr>');
		j = (diapason - longVib);
	}
	$("#affichage").append('</table>');
}

function format(){
	formata = document.getElementById("preformat");
	document.getElementById("diapas").value = formata.options[formata.selectedIndex].value;
}

function calcul(res){
	res = res / Math.pow(2, 1/12);
	return res;
}
function arrondir(resultat) {
      resultat = Math.round(resultat*localStorage.decimalPrecision)/localStorage.decimalPrecision;
      return resultat;
}

function fooNA(){
	var cordierM = parseInt($('#cordier_max').val());
	var cordierm = parseInt($('#cordier_min').val());
	var nbrFret = parseInt($('#nbreFretNA').val());
	var finger = parseInt($('#fingerNA').val());
	var SL = parseInt($('#diapasNA').val());
	
	t = SL;
	for(i=1;i<=nbrFret;i++){
		t = calcul(t);
	}
	
	var a = ((cordierM + cordierm) / 2) - (finger + 2); // fingerboard height + action height - average tailpiece height
	var b = t; // lenght between tail piece and point above last fret
	var result = arrondir((Math.asin(a/b)) * 180 / Math.PI);
	$("#affichageNA").empty();
	$("#affichageNA").append('<strong>' + result + ' (deg)</strong>');
}
